<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\ImportOrder;
use App\Admin\Renderable\ArticleTable;
use App\Admin\Renderable\BrandTable;
use App\Admin\Renderable\CourseTable;
use App\Admin\Renderable\FirmTable;
use App\Admin\Renderable\IdentityTable;
use App\Admin\Repositories\Banner;
use App\Models\AdHierarchy;
use App\Models\Article;
use App\Models\Course;
use App\Models\Firm;
use App\Models\FirmBrand;
use App\Models\UserIdentity;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;

class BannerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Banner(['hierarchy']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('sort');
            $grid->column('hierarchy.name','位置');
            $grid->column('banner_image')->image('',120,100);
            $grid->column('url','链接')
                 ->copyable();
            $grid->column('status')
                 ->using([1 => '显示',2 => '隐藏'])
                 ->label([1 => 'success',2 => 'danger']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('ad_hierarchy_id')
                      ->select(AdHierarchy::getBannerOption())->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Banner(), function (Show $show) {
            $show->field('id');
            $show->field('sort');
            $show->field('banner_image');
            $show->field('param_id');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Banner(), function (Form $form) {
            $form->display('id');
            $form->number('sort')
                 ->min(0)
                 ->help('数字越小排序越靠前显示')
                 ->required();
            $form->select('ad_hierarchy_id','选择位置')
                 ->options(AdHierarchy::getBannerOption())
                 ->required();

            $form->image('banner_image')
                ->disk('oss')
                 ->autoUpload()
                 ->removable(false)
                 ->uniqueName()
                 ->saveFullUrl()
                 ->required();

            $form->radio('position','跳转位置')
                 ->options([1 => '企业',2 => '品牌',3 => '文章',4  =>'课程',5 => '名人'])
                 ->when(1,function (Form $form){
                     $form->selectTable('firm_id','选择企业')
                          ->from(FirmTable::make())
                          ->model(Firm::class,'id','name');
                 })->when(2,function (Form $form){
                    $form->selectTable('brand_id','选择品牌')
                        ->from(BrandTable::make())
                        ->model(FirmBrand::class,'id','name');
                })->when(3,function (Form $form){
                    $form->selectTable('article_id','选择文章')
                        ->from(ArticleTable::make())
                        ->model(Article::class);
                })->when(4,function (Form $form){
                    $form->selectTable('course_id','选择课程')
                        ->from(CourseTable::make())
                        ->model(Course::class,'id','name');
                })->when(5,function (Form $form){
                    $form->selectTable('user_identity_id','选择名人')
                        ->from(IdentityTable::make())
                        ->model(UserIdentity::class,'id','name');
                });
            $form->hidden('url');
            $form->radio('status')
                 ->options([1 => '显示',2 => '隐藏'])
                 ->default(1)
                 ->required();

            $form->saving(function (Form $form){

                if ($form->input('position')){
                    switch ($form->input('position')){
                        case 1 :
                            if ($form->input('firm_id')){
                                $form->input('url','/pagesa/qiye/companyDetail/companyDetail?id='.$form->input('firm_id'));
                            }
                            break;
                        case 2 :
                            if ($form->input('brand_id')){
                                $form->input('url','/pagesa/qiye/detail/detail?id='.$form->input('brand_id'));
                            }
                            break;
                        case 3 :

                            if ($form->input('article_id')){
                                $form->input('url','/pagesa/wenzhang/detail/detail?id='.$form->input('article_id'));
                            }

                            break;
                        case 4 :

                            if ($form->input('course_id')){
                                $form->input('url','/pagesa/jiangshi/kechengdetail/kechengdetail?id='.$form->input('course_id'));
                            }

                            break;
                        case 5 :

                            if ($form->input('user_identity_id')){
                                $form->input('url','/pagesa/mingren/detail/detail?id='.$form->input('user_identity_id'));
                            }
                            break;
                    }

                }

                $form->deleteInput(['position','firm_id','brand_id','article_id','course_id','user_identity_id']);

            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
